package cn.lanqiao.book.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


    public class JdbcUtils {
        private static DataSource ds;
        static {
            Properties prop = new Properties();
            try {
                prop.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
                ds = DruidDataSourceFactory.createDataSource(prop);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 获取链接
        public static Connection getConnection(){
            Connection conn = null;
            try {
                conn =   ds.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return  conn;
        }
        //关闭链接  放回数据库连接池
        public  static void close(Connection conn){
            if(conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        // 获取数据源
        public  static DataSource  getDataSource(){
            return  ds;
        }
    }
